import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/helper.dart';
import 'package:yp/pages/mall_app/turntable_aword/state.dart';
import 'package:yp/pages/mall_app/turntable_aword/views/get_award.dart';
import 'package:yp/services/api.dart';

class TurntableAwardLogic extends GetxController {
  final state = TurntableAwardState();
  @override
  void onInit() {
    super.onInit();
    state.infoState = Get.arguments;
    state.currentIndex = int.parse(Get.parameters['index']);
    queryAwardList();
  }

  tabClick(int index) {
    if (state.currentIndex != index) {
      state.currentIndex = index;
      update();
    }
  }

  void queryAwardList() {
    EasyLoading.show();
    var paramsIntegral = {
      'access_token': SpUtil.getString('token'),
      'usr_num_id': SpUtil.getInt('empeNumId'),
      'turntable_id': state.infoState.source['linktarget'],
      'single_usr': 1,
      'ignore_thankyou': 1, //忽略谢谢参与
      'order_type': 1, //  排序类别，0--按最后更新时间倒排，1--按领取状态正排，再按最后更新时间倒排
      'page': state.pageIndex,
      'rows': 10,
    };
    Api.getTurntableAwardList(paramsIntegral).listen((event) {
      if (event['code'] == 0) {
        var data = event['usr_draw_logs'];
        if (state.pageIndex == 1) {
          state.awardList = data;
        } else if (data != null) {
          state.awardList.addAll(data);
        }
      }
      if (state.refreshCtrl.isLoading) {
        state.refreshCtrl.loadComplete();
      }
      if (state.refreshCtrl.isRefresh) {
        state.refreshCtrl.refreshCompleted();
      }
      EasyLoading.dismiss();
      update();
    });
  }

  onLoading() {
    state.pageIndex += 1;
    queryAwardList();
  }

  onRefresh() {
    state.pageIndex = 1;
    queryAwardList();
  }

  awardItemClick(item) {
    if (item['receive_flag'] == 1) {
      toast('您已领取过该奖品');
      return;
    }

    var configInfo = state.infoState.bingoRewardConfigure
        .where((element) => element['series'] == item['reward_series'])
        .first;
    // var configInfo = state.infoState.bingoRewardConfigure.where((element) {
    //   return element['series'] == item['series'];
    // }).first;
    if (configInfo == null) {
      return;
    }
    showDialog(
        context: Get.context,
        builder: (context) {
          return TurntableAwardGetView(
              configInfo: configInfo,
              data: item,
              getEvent: () {
                getAward(item);
              });
        });
  }

  void getAward(data) {
    EasyLoading.show(status: '领取中');
    var paramsIntegral = {
      'reward_id': data['series'],
      'access_token': SpUtil.getString('token'),
      'usr_num_id': SpUtil.getInt('empeNumId'),
      'turntable_id': state.infoState.source['linktarget'],
    };
    Api.getTurntableAward(paramsIntegral).listen((event) {
      if (event['code'] == 0) {
        Get.back();
        queryAwardList();
      }
      EasyLoading.dismiss();
      update();
    });
  }

  @override
  void dispose() {
    super.dispose();
    state.refreshCtrl.dispose();
  }
}
